package com.lanelu.hencoderplusstudy.view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import kotlin.math.cos
import kotlin.math.sin

private val RADIUS = 150f.dp
private val ANGLES = floatArrayOf(60f, 90f, 150f, 60f)
private val COLORS = listOf(
    Color.parseColor("#C2185B"),
    Color.parseColor("#00ACC1"),
    Color.parseColor("#558B2F"),
    Color.parseColor("#5D4037")
)
private val OFFSET_LENGTH = 20f.dp

/**
 * 饼图练习
 */
class PieView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG);//开启抗锯齿

//    init {
//        paint.strokeWidth = 3f.px
//        paint.style = Paint.Style.STROKE
//    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)

    }

    override fun onDraw(canvas: Canvas) {
        var startAngle = 0f
        for ((index, angle) in ANGLES.withIndex()) {
            paint.color = COLORS[index]
            if (index == 2) {
                canvas.save()
                canvas.translate(
                    (OFFSET_LENGTH * cos(Math.toRadians((angle / 2f + startAngle).toDouble()))).toFloat(),
                    OFFSET_LENGTH * sin(Math.toRadians((angle / 2f + startAngle).toDouble()))
                        .toFloat()
                )
            }
//            canvas.drawArc(
//                width / 2f - RADIUS,
//                height / 2f - RADIUS,
//                width / 2f + RADIUS,
//                height / 2f + RADIUS,
//                startAngle,
//                angle, true, paint
//            )
            canvas.drawArc(
                width / 2f - RADIUS,
                0f + 200f,
                width / 2f + RADIUS,
                RADIUS * 2 + 200f,
                startAngle,
                angle, true, paint
            )
            startAngle += angle

            if (index == 2) {
                canvas.restore()
            }

        }
    }


}